var express = require('express');
var router = express.Router();
var pool = require("../config/mysqlconfig")
// 获取数据
// http://localhost:3000/article/all
router.get('/all',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            
            let sql = "select * from article order by a_createat desc"
            conn.query(sql,function(error,results){
                if(error){
                    console.log("数据库连接失败")
                }
                res.send(results)
                conn.release()
            })
        }
    })
})
// 添加数据
// http://localhost:3000/article/add

router.post('/add',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            
            let title = req.body.a_title;
            let desc = req.body.a_desc;
            let content = req.body.a_content;
            let cate = req.body.a_cate;
            let create = new Date();
            let updated = new Date();
            let data={
                a_title:title,
                a_desc:desc,
                a_content:content,
                a_cate:cate,
                a_createat:new Date(create),
                a_updatedat:updated
            }
            let sql ="insert into article set ?"
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log("数据库连接失败")
                }
                res.send(results)
                conn.release()
            })
        }
    })
})
// 删除数据
// http://localhost:3000/article/del

router.delete('/del',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            let id = req.body.id;
            let sql = "delete from article where id=?"
            conn.query(sql,id,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

// 更新数据
// http://localhost:3000/article/update

router.put('/update',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            let id = req.body.id;
            let title = req.body.a_title;
            let desc = req.body.a_desc;
            let content = req.body.a_content;
            let cate = req.body.a_cate;

            let data=[title,desc,content,cate,new Date(),id]

            let sql = "update article set a_title=?,a_desc=?,a_content=?,a_cate=?,a_updatedat=? where id=?"
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

// 查找数据
// http://localhost:3000/article/select
router.post('/sel',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            let id = req.body.id
            let sql = "select * from article where id=?"
            conn.query(sql,id,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})


module.exports = router